<?php
require_once("Response.php");
class User{
	public $link;
	public $response;
	public function __construct(){
		$this->response = new Response();
		$this->link = create_connection();
	}

	public function get(){
		$innersql = "select id from users";
		$condition = "";
		// var_dump($_POST['data']);
		$sort = null;
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$page = $data['page']?intval(filter_sql($data['page'])):1;
			$limit = $data['limit']?intval(filter_sql($data['limit'])):20;
			$title = filter_sql($data['title']);
			$sort = filter_sql($data['sort']);
			$inner = [];
			if($title != null) $inner[] = " nickname like '%$title%'";
			$arrlength=count($inner);
			if($arrlength) $condition .= ' where';
			// var_dump($inner);
			for($x=0;$x<$arrlength-1;$x++)
				$condition .= $inner[$x] . ' and';
			if($arrlength>=1) $condition .= $inner[$arrlength-1];
			$condition .= " order by id";
			if($sort == '-id') $condition .= " desc";
			$innersql .= $condition;
			if($page && $limit) $innersql = $innersql . " limit ".($limit * ($page - 1)).",$limit";
		}else{
			$innersql .= " order by id limit 0,20 ";
		}
		// echo $innersql;
		$sql = "select count(*) as total from users" . $condition;
		$result=@execute_sql($this->link,'zhihu',$sql);
		if($result) {
			$total = intval(mysqli_fetch_assoc($result)['total']);
			$sql = "select id,nickname,email,answers,questions,last_login,status from users 
                inner join ($innersql) as temp_table using(id) order by id";
            if($sort == '-id') $sql .= " desc";
            // echo $sql;
			$result=@execute_sql($this->link,'zhihu',$sql);
			if($result){
				$data = array();//初始化数组;
			    while ($row = mysqli_fetch_assoc($result)) {
			    	$alter = new UserData();//实例化对象;
					$alter->id = $row['id'];
					$alter->nickname = $row['nickname'];
					$alter->email = $row['email'];
					$alter->answers = $row['answers'];
					$alter->questions = $row['questions'];
					$alter->last_login = $row['last_login'];
					$alter->status = $row['status'];
					$data[] = $alter;
				}
				$this->response->returnData(200,'success',["items"=>$data,"total"=>$total]);
			}else {
				$this->response->returnError();
			}
		} else {
			$this->response->returnError();
		}
	}

	public function add(){
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$username = filter_sql($data['username']);
			$password = md5(filter_sql($data['password']));
			$nickname = filter_sql($data['nickname']);
			$email = filter_sql($data['email']);
			$sex = filter_sql($data['sex']);
			$status = filter_sql($data['status']);
			$insert = "insert into users(username,passwd,nickname,email,sex,status)
			values('$username','$password','$nickname','$email','$sex','$status');";
			// echo $insert;
			if($result = @execute_sql($this->link,'zhihu',$insert)){
				$this->response->returnData(200,'success',null);
			}else{
				$this->response->returnError();
			}
		}else {
			$this->response->returnError();
		}
	}

	// public function delete(){
	// 	if(isset($_POST['data'])){
	// 		$data = $_POST['data'];
	// 		$id = filter_sql($data['id']);
	// 		$sql = "delete from users where id=$id";
	// 		echo $sql;
	// 		if($result = @execute_sql($this->link,'zhihu',$sql)){
	// 			$this->response->returnData(200,'success',null);
	// 		}else{
	// 			$this->response->returnError();
	// 		}
	// 	}else {
	// 		$this->response->returnError();
	// 	}
	// }

	public function update(){
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$id = filter_sql($data['id']);
			$status = filter_sql($data['status']);
			$sql = "update users set status='$status' where id=$id";
			// echo $sql;
			if($result = @execute_sql($this->link,'zhihu',$sql)){
				$this->response->returnData(200,'success',null);
			}else{
				$this->response->returnError();
			}
		}else {
			$this->response->returnError();
		}
	}
}
class UserData
{
	public $id;
	public $nickname;
	public $email;
	public $answers;
	public $questions;
	public $last_login;
	public $status;
}